<?php
function datetimeFormatter($date)
{
    $start_time = date("Y-m-01 00:00:00",strtotime($date));
    $end_time = date("Y-m-d 23:59:59",strtotime("$start_time +1 month -1 day"));

    return ['start_time' => $start_time, 'end_time' => $end_time];
}

function dateNewObject($datetime)
{
    $date = new DateTime($datetime);

    return $date->format('Y-m-d H:i:s.u');
}

/**
 * 验证手机号
 */
function checkMobile($mobile)
{
    return preg_match('/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/i', $mobile);
}

function carTypeTitle($type)
{
    switch ($type) {
        case '1':
            $title = '轿车';

            break;
        case '2':
            $title = 'SUV';

            break;
        case '3':
            $title = '新能源';

            break;
        case '4':
            $title = 'MPV';

            break;
        default:
            $title = '轿车';
    }

    return $title;
}

function answerTypeTitle($type)
{
    switch ($type) {
        case '1':
            $title = 'SINGLE';

            break;
        case '2':
            $title = 'MULTY';

            break;
        case '3':
            $title = 'QUESTION';

            break;
        default:
            $title = 'QUESTION';
    }

    return $title;
}

function carType()
{
    return ['1' => '轿车', '2' => 'SUV', '3' => '新能源', '4' => 'MPV'];
}

function statusSwitch()
{
    return ['1' => '开启', '2' => '关闭'];
}

function mergeSwitch()
{
    return ['1' => '合并', '2' => '不合并'];
}

function mergeErpRepairSparepartNot()
{
    return [
        ERP_REPAIR_SPAREPART_NOT_ONE,
        ERP_REPAIR_SPAREPART_NOT_TWO,
        ERP_REPAIR_SPAREPART_NOT_THREE,
        ERP_REPAIR_SPAREPART_NOT_FOUR,
        ERP_REPAIR_SPAREPART_NOT_FIVE,
        ERP_REPAIR_SPAREPART_NOT_SIX,
        ERP_REPAIR_SPAREPART_NOT_SEVEN,
        ERP_REPAIR_SPAREPART_NOT_EIGHT,
        ERP_REPAIR_SPAREPART_NOT_NINE,
        ERP_REPAIR_SPAREPART_NOT_TEN,
        ERP_REPAIR_SPAREPART_NOT_ELEVEN,
        ERP_REPAIR_SPAREPART_NOT_TWELVE,
        ERP_REPAIR_SPAREPART_NOT_THIRTEEN,
        ERP_REPAIR_SPAREPART_NOT_FOURTEEN,
        ERP_REPAIR_SPAREPART_NOT_FIFTEEN,
        ERP_REPAIR_SPAREPART_NOT_SIXTEEN,
        ERP_REPAIR_SPAREPART_NOT_SEVENTEEN,
        ERP_REPAIR_SPAREPART_NOT_EIGHTEEN,
        ERP_REPAIR_SPAREPART_NOT_NINETEEN,
        ERP_REPAIR_SPAREPART_NOT_TWENTY,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_ONE,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_TWO,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_THREE,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_FOUR,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_FIVE,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_SIX,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_SEVEN,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_EIGHT,
        ERP_REPAIR_SPAREPART_NOT_TWENTY_NINE,
        ERP_REPAIR_SPAREPART_NOT_THIRTY,
        ERP_REPAIR_SPAREPART_NOT_THIRTY_ONE,
    ];
}

function statusSwitchTitle($status)
{
    return $status == 1 ? '开启' : '关闭';
}

function usedStatusSwitchTitle($status)
{
    return $status == 1 ? '已核销' : '未核销';
}

function packageTypeTitle($type)
{
    return $type == 1 ? '销售套餐' : '售后套餐';
}

function path2url($path)
{
    $url = '';

    if ($path) {
        if (substr($path, 0, 1) == '/') {
            $url = env('QN_APP_URL').$path;
        } else {
            $url = env('QN_APP_URL').'/'.$path;
        }

    }

    return $url;
}

function pathsUrl($path)
{
    $url = [];

    if ($path) {
        foreach ($path as $key => $value) {
            if ($value) {
                if (substr($value['url'], 0, 1) == '/') {
                    $url[$key]['url'] = env('QN_APP_URL').$value['url'];
                } else {
                    $url[$key]['url'] = env('QN_APP_URL').'/'.$value['url'];
                }

            }
        }
    }

    return $url;
}

function successMessage($data = [], $message = '成功', $code = 200)
{
    return ['code' => $code, 'data' => $data, 'message' => $message];
}

function failureMessage($message = '失败', $code = 400)
{
    return ['code' => $code, 'message' => $message];
}

/**
 * 验证是否本地环境
 * @return bool
 */
function isLocalHost()
{
    if (env('APP_ENV') == 'local') {
        return true;
    } else {
        return false;
    }
}

function curlRequest($url, $data = '')
{
    $ch = curl_init();
    $header = array("Accept-Charset: utf-8");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $tmpInfo = curl_exec($ch);

    if (curl_errno($ch)) {
        return false;
    } else {
        return $tmpInfo;
    }
}